﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NCrawler.Extensions;

namespace ZCrawler
{
    public class ZCrawlDataSpliterFormatterProcessor : ZCrawlDataProcessor
    {
        public string ori_spliter { get; set; }
        public string formatter { get; set; }
        public override string Process(string str)
        {
            string result = "";
            int fromStart = 0;                          //0 for yes -1 for no 1 for no spliter
            int idx = formatter.IndexOf('#');
            if(idx == 0)
            {
                idx = formatter.LastIndexOf('#');
                if(idx == 0)
                    fromStart = 1;
                else fromStart = -1;
            }
            string spliter = "";
            if (fromStart == 0)
                spliter = formatter.Substring(0, idx);
            else if (fromStart == -1)
                spliter = formatter.Substring(idx);

            string[] fragments = str.Split(ori_spliter.ToArray(), StringSplitOptions.None);
            foreach (string frag in fragments)
            {
                result += formatter.FormatWith(frag);
            }
            result = result.Trim(spliter.ToCharArray());
            return result;
        }

        public override string Description
        {
            get
            {
                return "<Rule><OriginalSpliter>{0}</OriginalSpliter><Formatter>{1}</Formatter></Rule>".FormatWith(ori_spliter, formatter);
            }
            set
            {
                base.Description = value;
            }
        }
    }
}
